/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cungmua.vn.kiosk.activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cungmua.vn.kiosk.AbstractActivity;
import cungmua.vn.kiosk.R;
import cungmua.vn.kiosk.db.DatabaseHandler;
import cungmua.vn.kiosk.model.NhanVien;
import cungmua.vn.kiosk.network.CallServices;
import cungmua.vn.kiosk.utils.WriteLog;

/**
 * @author kelvin.nguyen
 * 
 */
public class ChangePassword extends AbstractActivity {
	private NhanVien nhanvien;
	private DatabaseHandler db;
	private EditText editTextMaOld;
	private EditText editTextMatKhauNew;
	private EditText editTextMatKhauNewConfirm;
	private Button btn_xac_nhan;
	private String old_pass, new_pass, confir_new_pass;
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_passsword_nhanvien);
		db = new DatabaseHandler(self);
		nhanvien=db.getAllNhanviens().get(0);
		initView();

	}

	public void initView() {
		editTextMaOld = (EditText) findViewById(R.id.editTextMaOld);
		editTextMatKhauNew = (EditText) findViewById(R.id.editTextMatKhauNew);
		editTextMatKhauNewConfirm = (EditText) findViewById(R.id.editTextMatKhauNewConfirm);
		btn_xac_nhan = (Button) findViewById(R.id.btn_xac_nhan);
	}

	public OnClickListener onclick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			old_pass = editTextMaOld.getText().toString();
			new_pass = editTextMatKhauNew.getText().toString();
			confir_new_pass = editTextMatKhauNewConfirm.getText().toString();
			if (old_pass.length() < 1) {
				editTextMaOld.setError("");
				return;
			} else if (new_pass.length() < 1) {
				editTextMatKhauNew.setError("");
				return;
			} else if (confir_new_pass.length() < 1) {
				editTextMatKhauNewConfirm.setError("");
				return;
			}

		}
	};

	public class changePassword extends AsyncTask<Void, Void, Integer> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(getParent());
			dialog.setMessage(getResources().getString(R.string.label_dialog));
			dialog.show();

		}

		@Override
		protected Integer doInBackground(Void... params) {

			String json=CallServices.changePassword(self, String.valueOf(nhanvien.getId()), old_pass, new_pass);
			
			WriteLog.d(TAG, json);
			
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
		}

	}
}
